package com.qys.livesMall.event.listener;

import com.qys.livesMall.anchor.model.entity.AnchorRewardConfig;
import com.qys.livesMall.anchor.service.H2AnchorService;
import com.qys.livesMall.app.user.service.AppUserLevelService;
import com.qys.livesMall.app.user.service.UserStatisticsInfoService;
import com.qys.livesMall.cache.RedisCache;
import com.qys.livesMall.finance.enums.AccountBusinessTypeEnum;
import com.qys.livesMall.finance.event.FinanceUserAccountEvent;
import com.qys.livesMall.finance.model.dto.FinancePayDTO;
import com.qys.livesMall.finance.model.dto.user.FinanceUserAccountDTO;
import com.qys.livesMall.finance.service.FinanceBusinessService;
import com.qys.livesMall.finance.util.BusinessNoUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;


@Slf4j
@RequiredArgsConstructor
public class FinanceUserAccountListener {

    private final UserStatisticsInfoService userStatisticsInfoService;
    private final AppUserLevelService userLevelService;
    private final FinanceBusinessService financeBusinessService;
    private final H2AnchorService anchorService;
    private final RedisCache redisCache;

    private final static String REDIS_LOCK_KEY = "AnchorReward_lock";

    /**
     * 用户余额变动监听
     * @param financeUserAccountEvent
     */
    @Async
    @Order
    @EventListener(FinanceUserAccountEvent.class)
    public void saveUserRechargeInfo(FinanceUserAccountEvent financeUserAccountEvent){
        FinanceUserAccountDTO financeUserAccountDTO= (FinanceUserAccountDTO) financeUserAccountEvent.getSource();
        userStatisticsInfoService.updateBalance(financeUserAccountDTO);
   /*     //用户更新等级
        userLevelService.adjustUserLevel(financeUserAccountDTO.getUserId());*/

        boolean lock = redisCache.lock(REDIS_LOCK_KEY + financeUserAccountDTO.getUserId(), 1, 2);
        if(!lock){
            return;
        }
        //主播累计收益奖励
        AnchorRewardConfig anchorRewardConfig = anchorService.cumulativeIncomeBonus(financeUserAccountDTO.getUserId());
        if(anchorRewardConfig != null){
            FinancePayDTO payDTO = new FinancePayDTO();
            payDTO.setUserId(financeUserAccountDTO.getUserId());
            payDTO.setBusinessType(AccountBusinessTypeEnum.CUMULATIVE_INCOME_BONUS);
            payDTO.setAmountIncurred(anchorRewardConfig.getAwardAmount());
            payDTO.setBusinessNo(BusinessNoUtil.get(AccountBusinessTypeEnum.CUMULATIVE_INCOME_BONUS));
            payDTO.setDescription("主播累计收益奖励");
            financeBusinessService.pay(payDTO);
            if (payDTO.getSuccess()) {
                anchorService.keepRewardRecords(anchorRewardConfig.getConId(), financeUserAccountDTO.getUserId());
                log.info("主播累计收益奖励成功, 奖励配置id:{}，userId:{}", anchorRewardConfig.getConId(), financeUserAccountDTO.getUserId());
            } else {
                log.info("主播累计收益奖励失败, 奖励配置id:{}，userId:{}，原因：{}", anchorRewardConfig.getConId(), financeUserAccountDTO.getUserId(), payDTO.getMessage());
            }
        }
        redisCache.deleteObject(REDIS_LOCK_KEY + financeUserAccountDTO.getUserId());
    }
}
